package com.seavenois.alife2;

import android.app.Dialog;
import android.app.DialogFragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

//TODO: When implementing dismiss function, set Game.java variable "viewReport" to "false" to save resources

public class DialogReport extends DialogFragment {

	//Scenario
	private byte scenario;
	//TextViews to show the info in the dialog
	private TextView labelWorkers, labelLostInHosting; //TODO: Assign and switch those two
	private TextView[] textColor = new TextView[7];
	private TextView[] textBuilding = new TextView[4];
	private TextView[] textColorData = new TextView[7];
	private TextView[] textBuildingData = new TextView[4];
	private TextView[] textAmmountData = new TextView[15];
	private TextView[] textMoneyData = new TextView[15];
	private ImageView[] barColor = new ImageView[7];
	private ImageView[] barBuilding = new ImageView[4];
	//The main layout, references in case it has to be pragmatically resized
	private LinearLayout layout;
	//Layouts containing different info
	private LinearLayout layoutReportArea, layoutReportBuilding, layoutReportMoney;
	//Areas and building arrays
	private int[] hostingDemand = {0, 0, 0, 0, 0, 0, 0};
	private int[] hostingCapacity = {0, 0, 0, 0, 0, 0, 0};
	private int[] buildingDemand = {0, 0, 0, 0, 0, 0, 0};
	private int[] buildingCapacity = {0, 0, 0, 0, 0, 0, 0};
	//Info about benefits and losses to be passed to the report activity. It will contain, in order:
	//input, lostSouls, lostInHsoting, lostInFormation, lostInReincarnation, reincarnations, workersNeeded + workersUnemployed,
	//workersLocal, workersResident, workersCommuting, workersExternal, workersUnemployed, energyNeeded, energyProduction, energyExternal
	private int[] moneyInfo = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	//Array with parameters to multiply moneyInfo to find out money benefits and losses
	private float[] parameters = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	//Soul rate to work out money per new soul
	private float soulRate = 0;
	//Buttons with common options, some will be hidden depending on the box type
	Button buttonClose;
	//Screen width to resize the layout
	int width;
	
	//Must be called before showing the dialog. It sets the variables that determine which parts of the dialog are to show or to hide
	//Arguments are in arrays for simplicity. Check code to see what is what
	public void updateData(byte _scen, int _width, int[] _hostingDemand, int[] _hostingCapacity, int[] _buildingDemand, int[] _buildingCapacity, int[] _moneyInfo, float _soulRate){
		scenario = _scen;
		width =  (int) (_width * 0.9);
		hostingDemand = _hostingDemand;
		hostingCapacity = _hostingCapacity;
		buildingDemand = _buildingDemand;
		buildingCapacity = _buildingCapacity;
		moneyInfo = _moneyInfo;
		soulRate = _soulRate;
		//First and second screen
		for (int i = 0; i < 7; i++){
			textColorData[i].setText(Integer.toString(hostingDemand[i]) + "/\n" + Integer.toString(hostingCapacity[i])); 
		}
		for (int i = 0; i < 4; i++){
			textBuildingData[i].setText(Integer.toString(buildingDemand[i]) + "/\n" + Integer.toString(buildingCapacity[i])); 
		}
		//Draw bar for areas (can do in a loop because different images
		float ci;
		if (hostingCapacity[0] != 0)
			ci = (float) hostingDemand[0] / (float) hostingCapacity[0];	//maroon
		else
			ci = 0;
		if (ci < .1)
			barColor[0].setImageResource(R.drawable.barmaroon0);
		else if (ci >= .1 && ci < .3)
			barColor[0].setImageResource(R.drawable.barmaroon20);
		else if (ci >= .3 && ci < .5)
			barColor[0].setImageResource(R.drawable.barmaroon40);
		else if (ci >= .5 && ci < .7)
			barColor[0].setImageResource(R.drawable.barmaroon60);
		else if (ci >= .7 && ci < .9)
			barColor[0].setImageResource(R.drawable.barmaroon80);
		else if (ci >= .9)
			barColor[0].setImageResource(R.drawable.barmaroon100);
		if (hostingCapacity[1] != 0)
			ci = (float) hostingDemand[1] / (float) hostingCapacity[1]; //pink
		else
			ci = 0;
		if (ci < .1)
			barColor[1].setImageResource(R.drawable.barpink0);
		else if (ci >= .1 && ci < .3)
			barColor[1].setImageResource(R.drawable.barpink20);
		else if (ci >= .3 && ci < .5)
			barColor[1].setImageResource(R.drawable.barpink40);
		else if (ci >= .5 && ci < .7)
			barColor[1].setImageResource(R.drawable.barpink60);
		else if (ci >= .7 && ci < .9)
			barColor[1].setImageResource(R.drawable.barpink80);
		else if (ci >= .9)
			barColor[1].setImageResource(R.drawable.barpink100);
		if (hostingCapacity[2] != 0)
			ci = (float) hostingDemand[2] / (float) hostingCapacity[2]; //yellow
		else
			ci = 0;
		if (ci < .1)
			barColor[2].setImageResource(R.drawable.baryellow0);
		else if (ci >= .1 && ci < .3)
			barColor[2].setImageResource(R.drawable.baryellow20);
		else if (ci >= .3 && ci < .5)
			barColor[2].setImageResource(R.drawable.baryellow40);
		else if (ci >= .5 && ci < .7)
			barColor[2].setImageResource(R.drawable.baryellow60);
		else if (ci >= .7 && ci < .9)
			barColor[2].setImageResource(R.drawable.baryellow80);
		else if (ci >= .9)
			barColor[2].setImageResource(R.drawable.baryellow100);
		if (hostingCapacity[3] != 0)
			ci = (float) hostingDemand[3] / (float) hostingCapacity[3]; //blue
		else
			ci = 0;
		if (ci < .1)
			barColor[3].setImageResource(R.drawable.barblue0);
		else if (ci >= .1 && ci < .3)
			barColor[3].setImageResource(R.drawable.barblue20);
		else if (ci >= .3 && ci < .5)
			barColor[3].setImageResource(R.drawable.barblue40);
		else if (ci >= .5 && ci < .7)
			barColor[3].setImageResource(R.drawable.barblue60);
		else if (ci >= .7 && ci < .9)
			barColor[3].setImageResource(R.drawable.barblue80);
		else if (ci >= .9)
			barColor[3].setImageResource(R.drawable.barblue100);
		if (hostingCapacity[4] != 0)
			ci = (float) hostingDemand[4] / (float) hostingCapacity[4]; //red
		else
			ci = 0;
		if (ci < .1)
			barColor[4].setImageResource(R.drawable.barred0);
		else if (ci >= .1 && ci < .3)
			barColor[4].setImageResource(R.drawable.barred20);
		else if (ci >= .3 && ci < .5)
			barColor[4].setImageResource(R.drawable.barred40);
		else if (ci >= .5 && ci < .7)
			barColor[4].setImageResource(R.drawable.barred60);
		else if (ci >= .7 && ci < .9)
			barColor[4].setImageResource(R.drawable.barred80);
		else if (ci >= .9)
			barColor[4].setImageResource(R.drawable.barred100);
		if (hostingCapacity[5] != 0)
			ci = (float) hostingDemand[5] / (float) hostingCapacity[5]; //green
		else
			ci = 0;
		if (ci < .1)
			barColor[5].setImageResource(R.drawable.bargreen0);
		else if (ci >= .1 && ci < .3)
			barColor[5].setImageResource(R.drawable.bargreen20);
		else if (ci >= .3 && ci < .5)
			barColor[5].setImageResource(R.drawable.bargreen40);
		else if (ci >= .5 && ci < .7)
			barColor[5].setImageResource(R.drawable.bargreen60);
		else if (ci >= .7 && ci < .9)
			barColor[5].setImageResource(R.drawable.bargreen80);
		else if (ci >= .9)
			barColor[5].setImageResource(R.drawable.bargreen100);
		if (hostingCapacity[6] != 0)
			ci = (float) hostingDemand[6] / (float) hostingCapacity[6]; //purple
		else
			ci = 0;
		if (ci < .1)
			barColor[6].setImageResource(R.drawable.barpurple0);
		else if (ci >= .1 && ci < .3)
			barColor[6].setImageResource(R.drawable.barpurple20);
		else if (ci >= .3 && ci < .5)
			barColor[6].setImageResource(R.drawable.barpurple40);
		else if (ci >= .5 && ci < .7)
			barColor[6].setImageResource(R.drawable.barpurple60);
		else if (ci >= .7 && ci < .9)
			barColor[6].setImageResource(R.drawable.barpurple80);
		else if (ci >= .9)
			barColor[6].setImageResource(R.drawable.barpurple100);
		//Building bars
		for (int i = 0; i < 4; i++){
			if (buildingCapacity[i] != 0)
				ci = (float) buildingDemand[i] / (float) buildingCapacity[i];
			else
				ci = 0;
			if (ci < .1)
				barBuilding[i].setImageResource(R.drawable.barwhite0);
			else if (ci >= .1 && ci < .3)
				barBuilding[i].setImageResource(R.drawable.barwhite20);
			else if (ci >= .3 && ci < .5)
				barBuilding[i].setImageResource(R.drawable.barwhite40);
			else if (ci >= .5 && ci < .7)
				barBuilding[i].setImageResource(R.drawable.barwhite60);
			else if (ci >= .7 && ci < .9)
				barBuilding[i].setImageResource(R.drawable.barwhite80);
			else if (ci >= .9)
				barBuilding[i].setImageResource(R.drawable.barwhite100);
		}
		//Third screen
		parameters[0] = soulRate;
		int value;
		for (int i = 0; i < 15; i ++){
			textAmmountData[i].setText(Integer.toString(moneyInfo[i]));
			//In some values there is no text
			if (i != 1 && i != 6 && i != 7 && i != 12){
				value = (int) (moneyInfo[i] * parameters[i]);
				if (value == 0)
					textMoneyData[i].setTextColor(Color.GRAY);
				else if (value < 0)
					switch (scenario){
						case Values.SCENARIO_HEAVEN:
							textMoneyData[i].setTextColor(Color.RED);
							break;
						case Values.SCENARIO_HELL:
							textMoneyData[i].setTextColor(Color.CYAN);
							break;
					}
				else if (value > 0)
					switch (scenario){
						case Values.SCENARIO_HEAVEN:
							textMoneyData[i].setTextColor(Color.CYAN);
							break;
						case Values.SCENARIO_HELL:
							textMoneyData[i].setTextColor(Color.RED);
							break;
					}
				textMoneyData[i].setText(Integer.toString(value) + "ł");
			}
		}
	}
	
	//Called each time the dialog is to be shown
	//Configures the view that will be contained in the dialog window
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		//Set layout for the dialog
		View view = inflater.inflate(R.layout.dialog_report, container, false);
		//Resize the main layout
		layout = (LinearLayout) view.findViewById(R.id.dailogReportLayout);
		layout.setMinimumWidth(width);
		//Assign area image and labels
		barColor[0] = (ImageView) view.findViewById (R.id.reportBarMaroon);
		barColor[1] = (ImageView) view.findViewById (R.id.reportBarPink);
		barColor[2] = (ImageView) view.findViewById (R.id.reportBarYellow);
		barColor[3] = (ImageView) view.findViewById (R.id.reportBarBlue);
		barColor[4] = (ImageView) view.findViewById (R.id.reportBarRed);
		barColor[5] = (ImageView) view.findViewById (R.id.reportBarGreen);
		barColor[6] = (ImageView) view.findViewById (R.id.reportBarPurple);
		textColor[0] = (TextView) view.findViewById (R.id.reportMaroonName);
		textColor[1] = (TextView) view.findViewById (R.id.reportPinkName);
		textColor[2] = (TextView) view.findViewById (R.id.reportYellowName);
		textColor[3] = (TextView) view.findViewById (R.id.reportBlueName);
		textColor[4] = (TextView) view.findViewById (R.id.reportRedName);
		textColor[5] = (TextView) view.findViewById (R.id.reportGreenName);
		textColor[6] = (TextView) view.findViewById (R.id.reportPurpleName);
		textColorData[0] = (TextView) view.findViewById (R.id.reportMaroonCapacity);
		textColorData[1] = (TextView) view.findViewById (R.id.reportPinkCapacity);
		textColorData[2] = (TextView) view.findViewById (R.id.reportYellowCapacity);
		textColorData[3] = (TextView) view.findViewById (R.id.reportBlueCapacity);
		textColorData[4] = (TextView) view.findViewById (R.id.reportRedCapacity);
		textColorData[5] = (TextView) view.findViewById (R.id.reportGreenCapacity);
		textColorData[6] = (TextView) view.findViewById (R.id.reportPurpleCapacity);
		//Assign building image and labels
		barBuilding[0] = (ImageView) view.findViewById (R.id.reportBarDoors);
		barBuilding[1] = (ImageView) view.findViewById (R.id.reportBarAcademys);
		barBuilding[2] = (ImageView) view.findViewById (R.id.reportBarTopias);
		barBuilding[3] = (ImageView) view.findViewById (R.id.reportBarStations);
		textBuilding[0] = (TextView) view.findViewById (R.id.reportDoorsName);
		textBuilding[1] = (TextView) view.findViewById (R.id.reportAcademysName);
		textBuilding[2] = (TextView) view.findViewById (R.id.reportTopiasName);
		textBuilding[3] = (TextView) view.findViewById (R.id.reportStationsName);
		textBuildingData[0] = (TextView) view.findViewById (R.id.reportDoorsCapacity);
		textBuildingData[1] = (TextView) view.findViewById (R.id.reportAcademysCapacity);
		textBuildingData[2] = (TextView) view.findViewById (R.id.reportTopiasCapacity);
		textBuildingData[3] = (TextView) view.findViewById (R.id.reportStationsCapacity);
		//Asign money report textViews
		textAmmountData[0] = (TextView) view.findViewById (R.id.ammountNewSouls);
		textAmmountData[1] = (TextView) view.findViewById (R.id.ammountLostSouls);
		textAmmountData[2] = (TextView) view.findViewById (R.id.ammountWhileHosting);
		textAmmountData[3] = (TextView) view.findViewById (R.id.ammountWhileFormation);
		textAmmountData[4] = (TextView) view.findViewById (R.id.ammountAtReincarnation);
		textAmmountData[5] = (TextView) view.findViewById (R.id.ammountReincarnations);
		textAmmountData[6] = (TextView) view.findViewById (R.id.ammountWorkers);
		textAmmountData[7] = (TextView) view.findViewById (R.id.ammountLocal);
		textAmmountData[8] = (TextView) view.findViewById (R.id.ammountResident);
		textAmmountData[9] = (TextView) view.findViewById (R.id.ammountCommuting);
		textAmmountData[10] = (TextView) view.findViewById (R.id.ammountImportedWorkers);
		textAmmountData[11] = (TextView) view.findViewById (R.id.ammountUnemployed);
		textAmmountData[12] = (TextView) view.findViewById (R.id.ammountEnergyConsumption);
		textAmmountData[13] = (TextView) view.findViewById (R.id.ammountProduced);
		textAmmountData[14] = (TextView) view.findViewById (R.id.ammountImportedEnergy);
		textMoneyData[0] = (TextView) view.findViewById (R.id.moneyNewSouls);
		textMoneyData[1] = (TextView) view.findViewById (R.id.moneyLostSouls);
		textMoneyData[2] = (TextView) view.findViewById (R.id.moneyWhileHosting);
		textMoneyData[3] = (TextView) view.findViewById (R.id.moneyWhileFormation);
		textMoneyData[4] = (TextView) view.findViewById (R.id.moneyAtReincarnation);
		textMoneyData[5] = (TextView) view.findViewById (R.id.moneyReincarnations);
		textMoneyData[6] = (TextView) view.findViewById (R.id.moneyWorkers);
		textMoneyData[7] = (TextView) view.findViewById (R.id.moneyLocal);
		textMoneyData[8] = (TextView) view.findViewById (R.id.moneyResident);
		textMoneyData[9] = (TextView) view.findViewById (R.id.moneyCommuting);
		textMoneyData[10] = (TextView) view.findViewById (R.id.moneyImportedWorkers);
		textMoneyData[11] = (TextView) view.findViewById (R.id.moneyUnemployed);
		textMoneyData[12] = (TextView) view.findViewById (R.id.moneyEnergyConsumption);
		textMoneyData[13] = (TextView) view.findViewById (R.id.moneyProduced);
		textMoneyData[14] = (TextView) view.findViewById (R.id.moneyImportedEnergy);
		//Populate the parameters array
		parameters[2] = (-1) * Values.LOOS_HOSTING;
		parameters[3] = (-1) * Values.LOOS_TRAINING;
		parameters[4] = (-1) * Values.LOOS_REINCARNATION;
		parameters[5] = Values.MONEY_REINCARNATION;
		parameters[8] = (-1) * Values.SALARY_RESIDENT;
		parameters[9] = (-1) * Values.SALARY_COMMUTING;
		parameters[10] = (-1) * Values.SALARY_IMPORTED;
		parameters[11] = (-1) * Values.UNEMPLOYMENT_CHECK;
		parameters[13] = (-1) * Values.ENERGY_LOCAL;
		parameters[14] = (-1) * Values.ENERGY_IMPORTED;
		
		//Assign static text
		textBuilding[0].setText(R.string.textDoors);
		textBuilding[1].setText(R.string.textAcademys);
		textBuilding[3].setText(R.string.textKarmaStations);
		switch (scenario){
			case Values.SCENARIO_HELL:
				textColor[0].setText(R.string.lust);
				textColor[1].setText(R.string.gluttony);
				textColor[2].setText(R.string.greed);
				textColor[3].setText(R.string.laziness);
				textColor[4].setText(R.string.anger);
				textColor[5].setText(R.string.envy);
				textColor[6].setText(R.string.vanity);
				textBuilding[2].setText(R.string.textDistopias);
				break;
			case Values.SCENARIO_HEAVEN:
				textColor[0].setText(R.string.chastity);
				textColor[1].setText(R.string.temperance);
				textColor[2].setText(R.string.charity);
				textColor[3].setText(R.string.diligence);
				textColor[4].setText(R.string.patience);
				textColor[5].setText(R.string.kindness);
				textColor[6].setText(R.string.humillity);
				textBuilding[2].setText(R.string.textUtopias);
				break;
		}
    	//Return the view that will be shown in the dialog
    	return view;
    }
    
	//Called each time the dialog is created
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = super.onCreateDialog(savedInstanceState);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		return dialog;
	}
}
